package com.luu.game.service.impl;

import com.luu.game.constant.RareConstant;
import com.luu.game.dao.ElfDao;
import com.luu.game.dao.RegionDao;
import com.luu.game.data.dto.*;
import com.luu.game.service.RobotService;
import com.luu.game.service.logic.elfRareFactory.BaseElfRare;
import com.luu.game.service.logic.elfRareFactory.RareFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

@Service
public class RobotServiceImpl implements RobotService {

    @Autowired
    private ElfDao elfDao;
    @Autowired
    private RegionDao regionDao;

    @Override
    public ElfInfoDTO createRobot(Integer level, Integer region) {
        String robotRare = getRobotRare(region);
        ElfInfoDTO robotInfo = randomProperty(robotRare, level);
        ElfInfoDTO result = getRobotNameAndSkill(region, robotRare, robotInfo);
        return result;
    }

    /**
     * 获取野怪名称和技能
     * @param region
     * @param rare
     * @return
     */
    private ElfInfoDTO getRobotNameAndSkill(Integer region, String rare, ElfInfoDTO elfInfoDTO){
        final List<ElfRegionDTO> list = elfDao.findElfByRegionId(region);
        ElfInfoDTO result = randomElf(list, rare, elfInfoDTO);
        List<ElfSkillDTO> skillList = elfDao.findSkillByElfId(result.getElfId());
        int nextInt = new Random().nextInt(skillList.size());
        ElfSkillDTO elfSkillDTO = skillList.get(nextInt);
        result.setSkillId(elfSkillDTO.getSkId());
        return elfInfoDTO;
    }

    /**
     * 随机精灵
     * @param list
     * @param rare
     * @param elfInfoDTO
     * @return
     */
    private ElfInfoDTO randomElf(List<ElfRegionDTO> list, String rare, ElfInfoDTO elfInfoDTO){
        int size = list.size();
        int nextInt = new Random().nextInt(size);
        ElfRegionDTO dto = list.get(nextInt);
        ElfDTO elfDTO = elfDao.findById(dto.getElfId());

        if (rare.equals(elfDTO.getRare())){
            elfInfoDTO.setName(elfDTO.getName());
            elfInfoDTO.setElfId(elfDTO.getId());
            elfInfoDTO.setProperty(elfDTO.getProperty());
            elfInfoDTO.setRare(elfDTO.getRare());
            return elfInfoDTO;
        }

        return randomElf(list, rare, elfInfoDTO);
    }

    /**
     * 随机获取精灵稀有度
     * @param region
     * @return
     */
    private String getRobotRare(Integer region){
        final RegionDTO regionDTO = regionDao.findByRegionId(region);
        final String regionRare = regionDTO.getRegionRare();
        final List<String> strings = Arrays.asList(regionRare.split(","));
        final String rare = randomRobotRare(strings);
        return rare;
    }

    /**
     * 随机野怪属性
     * @param rare
     * @return
     */
    private ElfInfoDTO randomProperty(String rare, Integer level){
        final BaseElfRare baseElfRare = new RareFactory(rare).createElfRare();
        final ElfInfoDTO result = baseElfRare.robotValueCreate(level);
        return result;
    }

    /**
     * 随机野怪稀有度
     * A 5%
     * B 15%
     * C 80%
     * @return
     */
    private String randomRobotRare(List rareList){
        String result = RareConstant.C;

        int nextInt = new Random().nextInt(RareConstant.ODDS);
        final double A_ODDS = RareConstant.ODDS - RareConstant.ODDS * RareConstant.A_ODDS;
        final double B_ODDS = RareConstant.ODDS - RareConstant.ODDS * RareConstant.B_ODDS;

        if (nextInt >= A_ODDS && rareList.contains(RareConstant.A))
            result = RareConstant.A;
        if (nextInt >= B_ODDS && nextInt < A_ODDS && rareList.contains(RareConstant.B) )
            result = RareConstant.B;

        return result;
    }
}
